
load ('exercise6.mat');

dim = size(data_x{1},2); % dimension of data

numOfDataSets = size(data_x,1) ; % total number of data sets

dataSetSize = size(data_x{1},1) ; % number of items in each data set

numOfIterations = 100 ;

numOfClusters = 3;

mg = zeros(numOfClusters,dim); % means of clusters

w = zeros(numOfDataSets,dim); % weight vectors of each task

index = zeros(1,30); % clusters of each task

index(1:10) = 1; index(11:20) = 2 ; index(21:30) = 3; % initial clusters

temp = diag(repmat(4,dim,1)) ;

for i= 1:numOfClusters
	rg(:,:,i) = temp; % cluster variances
end

temp = diag(repmat(2.25,dim,1));
variance = zeros(dim,dim,numOfDataSets);
for i= 1:numOfDataSets
	variance(:,:,i) = temp;
end

for iter = 1:numOfIterations
    %update rule for task's weight vector
    for i = 1:numOfDataSets
        w(i,:) = ((rg(:,:,index(i)) * data_x{i}' * data_y{i}) + variance(:,:,i) * mg(index(i),:)') \ (rg(:,:,index(i)) * data_x{i}' * data_x{i} + variance(:,:,i));
    end
    
    %update rule for variances
    for i = 1: numOfDataSets
        temp = zeros(dim,dim);
        total = temp;
        for j = 1:dataSetSize
            temp =(data_y{i}(j) - data_x{i}(j,:)*w(i,:)')*(data_y{i}(j) - data_x{i}(j,:)*w(i,:)')';
            total = total + temp;
        end
            
        variance(:,:,i) = total / dataSetSize;
    end
    
    
    %update rule for cluster means
    for i = 1:numOfClusters
        mg(i,:) = mean(w(index==i,:));
    end
    
    %finding new clusters for each task
    for i=1:numOfDataSets
        [dist ind] = min(sum(((repmat(w(i,:),3,1) - mg).^2),2)); 
        index(i) = ind;
    end
    
end

EstimatedClusters = index;
EstimatedClusters
TrueClusters = trueg';
TrueClusters